﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MovieDB
{
    public partial class FrmOptions : Form
    {
        private MdbOptions options;
        public event EventHandler OptionsCommitted;

        public string DbDirectory
        {
            get
            {
                return this.txtDbDirectory.Text == null ? string.Empty : this.txtDbDirectory.Text;
            }
        }

        public FrmOptions(MdbOptions options)
        {
            InitializeComponent();

            this.options = options;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(this.txtDbDirectory.Text))
            {
                this.options.DbDirectory = this.txtDbDirectory.Text;
            }
            else
            {
                MessageBox.Show("Das Verzeichnis " + this.txtDbDirectory.Text + " existiert nicht");
                return;
            }

            if (this.lstMovieFolders.Items.Count < 1)
            {
                MessageBox.Show("Bitte gib mindestens ein Filmverzeichnis an.");
                return;
            }
            else
            {
                foreach (object movieFolder in this.lstMovieFolders.Items)
                {
                    this.options.MovieFolders.Add((string)movieFolder);
                }
            }

            //this.options.SaveMovieFolders();

            if (this.OptionsCommitted != null)
            {
                this.OptionsCommitted(this, new EventArgs());
            }

            this.Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            this.fbdFolder.ShowDialog();
            this.txtDbDirectory.Text = this.fbdFolder.SelectedPath;
            this.txtDbDirectory.BackColor = Color.White;
        }

        private void FrmOptions_Load(object sender, EventArgs e)
        {
            this.txtDbDirectory.Text = this.options.DbDirectory;

            if (this.options.DbDirectory == string.Empty)
            {
                this.txtDbDirectory.BackColor = Color.Red;
            }

            if (this.options.MovieFolders.Count < 1)
            {
                this.lstMovieFolders.BackColor = Color.Red;
            }
            else
            {
                foreach (string movieFolder in this.options.MovieFolders)
                {
                    this.lstMovieFolders.Items.Add(movieFolder);
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            this.lstMovieFolders.Items.Remove(this.lstMovieFolders.SelectedItem);
        }

        private void btnAddMovieFolder_Click(object sender, EventArgs e)
        {
            this.fbdFolder.ShowDialog();
            this.lstMovieFolders.Items.Add(this.fbdFolder.SelectedPath);
        }

    }
}
